// 获取应用实例
const app = getApp()

Page({
    data: {
        formData: {
            simple_name: '',
            name: '',
            contact: '',
            contact_phone: '',
            type: null
        },
        sel_type_name: '',
        type_list: [
            {id: 1, name: '成品', value: 1},
            {id: 2, name: '加工', value: 2},
        ],
    },
    onLoad() {
        wx.setNavigationBarTitle({
            title: '添加货品供应商'
        });
    },

    // 输入框改变
    formInputChange(e) {
        const { field } = e.currentTarget.dataset
        this.setData({
            [`formData.${field}`]: e.detail.value.trim()
        });
    },

    // 当类型改变时
    typeChange: function (e) {
        console.log(e)
        const index = e.detail.value;
        this.setData({
            sel_type_name: this.data.type_list[index].name,
            [`formData.type`]: this.data.type_list[index].value,
        })
    },

    // 提交
    formSubmit: function (e) {
        const __this = this;
        // 验证数据
        if (__this.data.formData.simple_name == '' || __this.data.formData.type == null
        || __this.data.formData.contact == '' || __this.data.formData.contact_phone == '') {
            wx.showToast({
                title: '必填项不能为空',
                icon: 'none',
                duration: 1500,
            });
            return false;
        }
        // 发起请求
        let result = app.request_api('admin/Factory/add_api', __this.data.formData);
        result.then(res => {
            // 向上一级页面传递参数
            var eventChannel = __this.getOpenerEventChannel();
            eventChannel.emit('changeData', __this.data.formData);
            // 跳转回前一页
            wx.showModal({
                title: '提示',
                content: '新增成功',
                showCancel: false,
                complete() {
                    wx.navigateBack();
                }
            });
        });
    },



})